﻿using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LY.DataAccess.Repository
{
    public class MemberLogRepository : BaseRepository
    {
        /// <summary>
        /// 会员登录日志列表
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        public PagedList<U_MemberLoginLog> QueryMemberLoginList(PageView view)
        {
            string cols = @" [ID],[MemberName],LoginTime,[Status],[IP],Remark ";
            return base.PageGet<U_MemberLoginLog>(view, cols,
                @"U_MemberLoginLog "
                , "  "
                , " LoginTime desc  ",
                "");

        }


        public PagedList<U_MemberLoginLog> QueryMemberLoginList(Entity.SearchEntity.MemberLoginSearch search, PageView view)
        {
            string cols = @" [ID],[MemberName],LoginTime,[Status],[IP],Remark ";

            string sqlWhere = "";
            if (!string.IsNullOrEmpty(search.MemberName))
            {
                sqlWhere += " and MemberName like '%"+search.MemberName+"%'";
            }
             
            return base.PageGet<U_MemberLoginLog>(view, cols,
                @"U_MemberLoginLog "
                , sqlWhere
                , " LoginTime desc  ",
                "");
            
        }

        /// <summary>
        /// 检索模块列表
        /// </summary>
        /// <returns></returns>
        public List<S_Module> QueryUserModuleList()
        {
            string sql = @" SELECT *
                       FROM [S_Module] order by path ";
            //return base.Query<S_Module>(sql, new { ID = id });
            return base.Query<S_Module>(sql,null);
        }

        /// <summary>
        /// 删除模块信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteModule(int id)
        {
            string sql = " DELETE FROM [S_Module] WHERE ID=@ID";
            return base.Excute(sql, new { ID = id });
        }

        /// <summary>
        /// 检索模块信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public S_Module GetModule(int? id)
        {
            string sql = @" SELECT *
                       FROM [S_Module] WHERE Id=@ID";
            return base.Get<S_Module>(sql, new { ID = id });
        }


        public U_MemberLoginLog GetLastLoginLog(int memberId)
        {
            string sql = @" SELECT  top 1 
                       FROM [U_MemberLoginLog] WHERE MemberId=@memberId order by LoginTime desc ";
            return base.Get<U_MemberLoginLog>(sql, new { memberId = memberId });
        }
    }
}
